/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var HotbiteCart = {
    overrideButtonsInThePage: function() {
        $('.add-to-cart-btn').unbind('click').click(function() {
            $('.cart').slideUp(300);
            var id = $(this).attr('rel').replace('product_', '');
            var qty = parseInt($('.h_product_qty_' + id).val());
            
            if (isNaN(qty) || qty < 1) {
                qty = 1;
                $('.qty_' + id).val(qty);
            }
            HotbiteCart.add(id, $('.h_product_size_' + id).val(), qty);
            
            return false;
        });
        
        $('[class^=size-list_]').unbind('change').change(function(){
            var id = $(this).attr('rel').replace('product_', '');
            $('.h_product_size_' + id).val($(this).val());
            
            HotbiteCart.updatePrice(id, $('.h_product_size_' + id).val());
            
            return false;
        });
        
        $('[class^=qty_]').unbind('change').change(function() {
            var id = $(this).attr('rel').replace('product_', '');
            $('.h_product_qty_' + id).val($(this).val());
            
            return false;
        });
    },
    
    add: function(id, size, qty){
        
        $.post('/order/add', {
            id: id,
            size: size,
            qty: qty
        }, function(data) {
            if (data.success) {
                $('.cart').load('/template/cart.jsp').slideDown(200);
            } else {
                location.href=data.redirect;
            }
        }, 'json');
        
    },
    
    updatePrice: function(id, size) {
        $.post('/product/pizza', {
            id: id,
            size: size
        }, function(data) {
            if (data.success) {
                $('.price_' + id).html("");
                $('.price_' + id).fadeIn(200).html(data.price.toFixed(2));
            } else {
                location.href=data.redirect;
            }
        }, 'json');
    }
}

$(document).ready(function() {
    HotbiteCart.overrideButtonsInThePage();
});
